<?php
namespace app\index\controller;
use app\common\controller\Api;

class UserInfo extends Api
{

    /**
     * @var \app\index\model\UserInfo
     */
    protected $model = null;

    public $relationSearchArr = [];

    public $join_arr = ['user_finance','user_info.id = user_finance.user_id','LEFT'];

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('UserInfo');
    }

    public function _condition($condition){

        $this->relationSearchArr = ['userFinance', 'channelInfo', 'campaignInfo'];

        $condition['channel_id'] = $this->request->param('channel_id');
        $condition['share_user_id'] = $this->request->param('share_user_id');
        $condition['keyword'] = $this->request->param('keyword');
        $condition['is_subscribe'] = $this->request->param('is_subscribe', '');
        $condition['use'] = $this->request->param('use');
        $condition['start_create_time'] = $this->request->param('start_create_time');
        $condition['end_create_time'] = $this->request->param('end_create_time');

        $filer_arr = [];//and
        $op_arr = [];//and

        $filer_or_arr = [];//or
        $op_or_arr = [];//or

        if($condition['channel_id']){
            $filer_arr['channel_id'] = $condition['channel_id'];
        }
        if($condition['share_user_id']){
            $filer_arr['share_user_id'] = $condition['share_user_id'];
        }
        if($condition['keyword']){
            $filer_or_arr['id'] = $condition['keyword'];
            $filer_or_arr['wx_open_id'] = $condition['keyword'];
            $filer_or_arr['wx_union_id'] = $condition['keyword'];
            $filer_or_arr['mobile'] = is_numeric($condition['keyword'])?$condition['keyword']:1;
            $filer_or_arr['user_nickname'] = $condition['keyword'];
            $filer_or_arr['wx_nickname'] = $condition['keyword'];
            $op_or_arr['user_nickname'] = 'LIKE';
            $op_or_arr['wx_nickname'] = 'LIKE';
        }
        if(strlen($condition['is_subscribe'])>0){
            $filer_arr['is_subscribe'] = $condition['is_subscribe'];
        }
        if(strlen($condition['use'])>0){
            switch ($condition['use']) {
                case 1:
                    $filer_arr['use_yday'] = 1;
                    break;

                case 7:
                    $filer_arr['use_7day'] = 1;
                    break;

                case 30:
                    $filer_arr['use_30day'] = 1;
                    break;

                case 0:
                    $filer_arr['use_yday'] = 0;
                    $filer_arr['use_7day'] = 0;
                    $filer_arr['use_30day'] = 0;
                    break;
                default:
                    # code...
                    break;
            }
        }
        if($condition['start_create_time'] || $condition['end_create_time']){
            $filer_arr['create_time'] = $condition['start_create_time'].','.$condition['end_create_time'];
            $op_arr['create_time'] = 'BETWEEN'; 
        }
        return array($filer_arr, $op_arr, $filer_or_arr, $op_or_arr);
    }
}
